package hieuph.facerecognition.ManipulationPicture;

import hieuph.facerecognition.mainUi.MainActivity;

import org.opencv.core.Rect;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class MyImageView extends View {
	public static int LONG_PRESS_TIME = 500; // Time in miliseconds
	private static final int DETECTED_FACE = 1;
	private static final int ERASE_DETECTED_FACE = 2;
	private static final int LOADIMAGE = 3;

	private Bitmap myBitmap, resultBmp;
	private Rect[] facesArray;
	private int mode;
	private Paint paint = new Paint();

	// private Paint btmPaint = new Paint();
	private int widthofBitMap, heightofBitMap;
	float ratioWidth, ratioHeight;
	float[] left;
	float[] right;
	float[] top;
	float[] bottom;
	Context mContext;

	// final Handler _handler = new Handler();
	// Runnable _longPressed = new Runnable() {
	// public void run() {
	// Toast.makeText(mContext, "hehe", Toast.LENGTH_SHORT).show();
	// }
	// };

	public MyImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		// TODO Auto-generated constructor stub
		paint.setColor(Color.RED);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(3);

	}

	// this method is used to set the defaultbitmap to our view.
	public void setImage(Bitmap bitmap) {
		if (myBitmap != null) {
			myBitmap = null;
		}
		myBitmap = bitmap;
		resultBmp = scaleBitmap(myBitmap, MainActivity.screenWidth);
		mode = LOADIMAGE;
		invalidate();
	}

	public void clearBimap() {
		if (myBitmap != null) {
			myBitmap = null;
		}
	}

	public Bitmap getMyBitmap() {
		return myBitmap;
	}

	// method to detect faces in a bitmap.
	public void facedetect(Rect[] facesArray) {
		this.facesArray = facesArray;
		mode = DETECTED_FACE;
		invalidate();
	}

	public void EarseRect() {
		mode = ERASE_DETECTED_FACE;
		invalidate();
	}

	public Bitmap scaleBitmap(Bitmap myBitmap, int screenWidth) {
		Bitmap resultBmp = null;
		if (myBitmap != null) {
			int w = myBitmap.getWidth();
			int h = myBitmap.getHeight();

			widthofBitMap = screenWidth;
			heightofBitMap = widthofBitMap * h / w;
			ratioHeight = (float) heightofBitMap / h;
			ratioWidth = (float) widthofBitMap / w;

			// we are setting the bitmap according to the width of the bitmap.
			// the width of bitmap is equal to width of the screen.
			resultBmp = Bitmap.createScaledBitmap(myBitmap, widthofBitMap,
					heightofBitMap, true);
		}
		return resultBmp;
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		super.onSizeChanged(w, h, oldw, oldh);
		// bitmapDetect = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			float x = event.getX();
			float y = event.getY();
//			Log.e("Toa do", "x:" +  x + " y:" + y );
			if (facesArray != null) {
				for (int i = 0; i < facesArray.length; i++) {
					if ((left[i] < x) && (x < right[i]) && (bottom[i] > y)
							&& (y > top[i])) {
						
						// Cat anh o day
						
					}
				}
			}
			break;
		case MotionEvent.ACTION_MOVE:

			break;
		case MotionEvent.ACTION_UP:

			break;
		}
		return true;
	}

	protected void onDraw(Canvas canvas) {
		if (myBitmap != null) {
			canvas.drawBitmap(resultBmp,
					(MainActivity.screenWidth - widthofBitMap) / 2,
					(MainActivity.screenHeight - heightofBitMap) / 2, null);
			switch (mode) {
			case DETECTED_FACE:
				Log.e("hai", facesArray.length + "");
				left = new float[facesArray.length];
				right = new float[facesArray.length];
				top = new float[facesArray.length];
				bottom = new float[facesArray.length];
				for (int i = 0; i < facesArray.length; i++) {
					left[i] = (MainActivity.screenWidth - widthofBitMap) / 2
							+ facesArray[i].x * ratioWidth;
					right[i] = (MainActivity.screenWidth - widthofBitMap) / 2
							+ facesArray[i].x * ratioWidth
							+ facesArray[i].width * ratioWidth;
					top[i] = (MainActivity.screenHeight - heightofBitMap) / 2
							+ facesArray[i].y * ratioHeight;
					bottom[i] = (MainActivity.screenHeight - heightofBitMap)
							/ 2 + facesArray[i].y * ratioHeight
							+ facesArray[i].height * ratioHeight;
					canvas.drawRect(left[i], top[i], right[i], bottom[i], paint);
//					Log.e("Toa do", "left:" +  left[i] + " right:" + right[i] + " top:" + top[i] + " bottom:" + bottom[i]);
				}

				break;
			case ERASE_DETECTED_FACE:
				break;
			default:
				break;
			}

		}
	}
}
